/*
 * Copyright (C) 2010 M. Hautle.
 * 
 * This file is part of SimpleEL.
 * 
 * SimpleEL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * SimpleEL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with SimpleEL. If not, see <http://www.gnu.org/licenses/>.
 */
package ch.msoftch.internal;

import java.util.*;

/**
 * Classloader grouping several loaders together.
 * 
 * @author M. Hautle
 */
public class CompositeClassLoader extends ClassLoader {
    /** The other loaders. */
    private final ClassLoader[] loaders;

    /**
     * Default constructor
     * 
     * @param parent The parent loader - the one queried first
     * @param loaders The other loaders
     */
    public CompositeClassLoader(ClassLoader parent, ClassLoader... loaders) {
        super(parent);
        this.loaders = Arrays.copyOf(loaders, loaders.length);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected final Class<?> findClass(String name) throws ClassNotFoundException {
        for (ClassLoader l : loaders)
            try {
                return l.loadClass(name);
            } catch (ClassNotFoundException e) {
                // we try first all other loaders
            }
        return super.findClass(name);
    }
}
